// pages/photoRepository/photoRepository.js

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      //图标
      iconPath: app.globalData.iconPath,
      //访问java后台
      goJavaBackstagePath: app.globalData.goJavaBackstagePath,
      //测试数据图片
      testDataImgPath: app.globalData.testDataImgPath,


      product:null,
      photoList:[],
      iconList:{},
      wasSelectCount:0,//已经选取的数量
      pre: 1,
      currentPhoto: '',//当前预览的第几张
      photoViewPath: '',//当前预览图片的路径
     
      reductionBorder:' 1rpx solid #BBFFFF',

      //根据productId 是否为空判断是否是从订单详情页面进入 如果不是则切换视图，无法进行任何操作
      productId: null,
      orderId: null,
      orderStatus:null,
     
  },

  //循环读取已经选取的照片数量
  wasSelectCount() {
    if(this.data.product!=null){
      var count = 0;
      for (var i = 0; i < this.data.product.photos.length; i++) {
        count = count + this.data.product.photos[i].photoSelectCount;
      }
      this.setData({
        wasSelectCount: count
      });
    }    
  },

  //循环将已选照片  体现在照片库照片上
  assignmentToPhotoLsit() {  
    if(this.data.product!=null){
      for (var i = 0; i < this.data.product.photos.length; i++) {
        for (var j = 0; j < this.data.photoList.length; j++) {
          if (this.data.product.photos[i].id == this.data.photoList[j].id) {
            this.data.photoList[j].photoSelectCount = this.data.product.photos[i].photoSelectCount;
          }
        }
      }
      this.setData({
        photoList: this.data.photoList,
      });
    }   
  },
  //选取照片
  selectPhoto(id) {
    var flag = true;
    if (this.data.wasSelectCount < this.data.product.optionalPhotoNum) {
      for (var i = 0; i < this.data.product.photos.length; i++) {
        if (this.data.product.photos[i].id == id) {
          this.data.product.photos[i].photoSelectCount++;
          var that = this;
          //发送到后台对产品-图片中间表中添加图片
          // wx.request({
          //   url: this.data.goJavaBackstagePath+'/product/addPhotoToProPhoto',
          //   data: { orderId:that.data.orderId, productId: that.data.productId,photoId:that.data.product.photos[i].id },
          //   header: {
          //     'content-type': 'application/json'
          //   },
          //   success: function (res) {
          //     console.log(res.data)
          //   }
          // })
          flag = false;
        }
      }
      if (flag) {
        var addPhoto = { id: id, photoSelectCount: 1 };
        this.data.product.photos.push(addPhoto);
        console.log(this.data.product);
      }
      this.setData({
        product: this.data.product,
      });
      this.wasSelectCount();
      this.assignmentToPhotoLsit();
    } else {
      wx.showModal({
        title: '提示',
        content: "图片已选满！",
      })
    }
  },
  //删除选择的照片
  deletePhoto(id){
    var photosWillDelete = null;
    for (var i = 0; i < this.data.photoList.length; i++) {
      if (this.data.photoList[i].id == id) {
        if (this.data.photoList[i].photoSelectCount > 1) {
          this.data.photoList[i].photoSelectCount--;
          var selectNum = this.data.photoList[i].photoSelectCount;
          for (var j = 0; j < this.data.product.photos.length; j++) {
            if (this.data.product.photos[j].id == id) {
              var that = this;
              //到后台改变数量  根据照片id、orderId 、产品id 查询出相同照片的集合，删除第一个
              // wx.request({
              //       url: this.data.goJavaBackstagePath+'/product/deleteSelectedPhotoFromList',
              //       data: { orderId: that.data.orderId,productId:that.data.productId, photoId: that.data.product.photos[j].id},
              //       header: {
              //         'content-type': 'application/json'
              //       },
              //       success: function (res) {
              //         console.log(res.data)
              //       }
              // });
              this.data.product.photos[j].photoSelectCount = selectNum;
            }
          }
        } else if (this.data.photoList[i].photoSelectCount == 1) {
          this.data.photoList[i].photoSelectCount--;
          for (var j = 0; j < this.data.product.photos.length; j++) {
            if (this.data.product.photos[j].id == id) {
              //到后台改变数量  根据照片id、orderId 、产品id 查询出相同照片的集合，删除第一个
              // wx.request({
              //       url: this.data.goJavaBackstagePath+'/product/deleteSelectedPhotoFromList',
              //       data: { orderId: that.data.orderId,productId:that.data.productId, photoId: that.data.product.photos[j].id},
              //       header: {
              //         'content-type': 'application/json'
              //       },
              //       success: function (res) {
              //         console.log(res.data)
              //       }
              // });
              photosWillDelete = j;
            }
          }
          this.data.product.photos.splice(photosWillDelete, 1);
        }else{
          wx.showModal({
            title: '提示',
            content: "当前图片未选取！",
          })
        }
      }
    }
    this.setData({
      product: this.data.product,
      photoList: this.data.photoList,
    });
    this.wasSelectCount();
    this.assignmentToPhotoLsit();
  },

  //页面缩略 点击添加  选择照片
  thumbnailAdd(event){  
    var id = event.target.dataset.id;
    this.selectPhoto(id);
  },

  //页面缩略图 点击删除选择的照片
  thumbnailDelete(event){
    var id = event.target.dataset.id;
    this.deletePhoto(id);
  },

  //页面缩略图 点击删除 删除照片库中的照片
  thumbnaildeleteRepositoryPhoto(event){
    var delid = event.target.dataset.delid;
    var delindex = event.target.dataset.delindex;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您将删除图片库中的这张图片，同时将删除所有产品中已经选择的这张图片！',
      success:function(res){
        if(res.confirm){
          //---------到后台去删除所有产品中选择的该图片 以及照片库中的照片 传图片id
          // wx.request({
          //       url: this.data.goJavaBackstagePath+'/order/deletePhotoFromRepository',
          //       data: { orderId: that.data.orderId, photoId: delid },
          //       header: {
          //         'content-type': 'application/json'
          //       },
          //       success: function (res) {
          //         console.log(res.data)
          //       }
          // });
          that.data.photoList[delindex].status = 0;
          that.data.photoList[delindex].photoSelectCount=0;            
            //将product 数据变更
            if (that.data.product!=null){
              var delPhotoIndex = null;
              for (var i = 0; i < that.data.product.photos.length; i++) {
                if (that.data.product.photos[i].id == delid) {
                  delPhotoIndex = i;
                }
              }              
              that.data.product.photos.splice(delPhotoIndex, 1);
            }           
            that.setData({
              photoList:that.data.photoList,
              product:that.data.product
            });
            that.wasSelectCount();
            that.assignmentToPhotoLsit();
        } else if (res.cancel){
          console.log("用户点击取消！");
        }
      }
    })
  },

  //预览删除照片库照片
  deleteRepositoryPhoto(event){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您将删除图片库中的这张图片，同时将删除所有产品中已经选择的这张图片！',
      success: function (res) {
        if (res.confirm) {
          var id = that.data.photoList[that.data.currentPhoto - 1].id;
  
          //-----到后台去删除所有产品中选择的该图片 以及照片库中的照片 传图片id
          // wx.request({
          //       url: this.data.goJavaBackstagePath+'/order/deletePhotoFromRepository',
          //       data: { orderId: orderId, photoId: id },
          //       header: {
          //         'content-type': 'application/json'
          //       },
          //       success: function (res) {
          //         console.log(res.data)
          //       }
          // });
          //改变页面photoList 中照片状态
          that.data.photoList[that.data.currentPhoto - 1].status=0;
          that.data.photoList[that.data.currentPhoto - 1].photoSelectCount = 0;
          //将product  中数据变更
          if(that.data.product!=null){
            var delPhotoIndex = null;
            for (var i = 0; i < that.data.product.photos.length; i++) {
              if (that.data.product.photos[i].id == id) {
                delPhotoIndex = i;
              }
            }
            that.data.product.photos.splice(delPhotoIndex, 1);
          }          
          that.setData({
            photoList: that.data.photoList,
            product: that.data.product
          });
          that.wasSelectCount();
          that.assignmentToPhotoLsit();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //自定义图片预览
  previewPhoto(event) {
    this.setData({
      pre: 0,
    
      currentPhoto: event.target.dataset.index + 1,
      
      photoViewPath: this.data.photoList[event.target.dataset.index].url
    });
  },

  //退出自定义图片预览
  outPre(event) {
    this.setData({
      pre: 1,   
      currentPhoto: '',
      photoViewPath: '',     
    });
  },
 
  //预览时选取照片
  previewSelectPhoto(event){
    var id = this.data.photoList[this.data.currentPhoto-1].id;
    this.selectPhoto(id);
  },

  //预览时删除选择的照片
  previewDeletePhoto(event){
    var id = this.data.photoList[this.data.currentPhoto - 1].id;
    this.deletePhoto(id);
  },
  //上一张
  goPrevious(event) {
    if (this.data.currentPhoto == 1) {
      wx.showModal({
        title: '提示',
        content: '这是第一张图片!',
      })
    } else {
      this.setData({
        currentPhoto: this.data.currentPhoto - 1,
        photoViewPath: this.data.photoList[this.data.currentPhoto - 2].url
      });
    }
  },

  //下一张
  goNext(event) {
    if (this.data.currentPhoto == this.data.photoList.length) {
      wx.showModal({
        title: '提示',
        content: '这是最后一张图片!',
      })
    } else {
      this.setData({
        currentPhoto: this.data.currentPhoto + 1,
        photoViewPath: this.data.photoList[this.data.currentPhoto].url
      });
    }
  },
  //将删除的照片库照片还原
  reductionPhoto(event){
    var photoId = event.target.dataset.photoid;
    var index = event.target.dataset.index;
    var that = this;
    //发送请求到后
     // wx.request({
    //   url: this.data.goJavaBackstagePath+'/product/getProductPhotoRepository',
    //   data: { order_id:that.data.orderId, photo_id: photoId },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })

    this.data.photoList[index].status=1;
    this.setData({
      photoList:this.data.photoList
    });
  },
  // 底部菜单----------------------------
  //跳转到订单列表
  goOrderList(event) {
    wx.redirectTo({
      url: '../order/order',
    })
  },

  //跳转到产品列表页面
  goProductList(event) {
    wx.redirectTo({
      url: '../product/product?orderId='+this.data.orderId+'&orderStatus='+this.data.orderStatus,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId = options.orderId;
    var orderStatus = options.orderStatus;
    var productId = options.productId;
    this.setData({
      orderId: orderId,
      productId: productId,
      orderStatus: orderStatus

    });

    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/product/getProductPhotoRepository',
    //   data: { orderId: orderId, productId: productId },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })
    //后台读取返回的数据
  var data = {
    photoList: [
      { id: 1, photoSelectCount:0,status:0,  url:this.data.testDataImgPath +'/mei1.jpg'},
      { id: 2, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei2.jpg'},
      { id: 3, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei3.jpg'},
      { id: 4, photoSelectCount: 0, status: 0,  url:this.data.testDataImgPath +'/mei4.jpg'},
      { id: 5, photoSelectCount: 0, status: 0,  url:this.data.testDataImgPath +'/mei5.jpg'},
      { id: 6, photoSelectCount: 0, status: 1,  url:this.data.testDataImgPath +'/mei6.jpg'},
      { id: 7, photoSelectCount: 0, status: 1,  url:this.data.testDataImgPath +'/mei7.jpg'},
      { id: 8, photoSelectCount: 0, status: 1,  url:this.data.testDataImgPath +'/mei8.jpg'},
      { id: 9, photoSelectCount: 0, status: 1,  url:this.data.testDataImgPath +'/mei9.jpg'},
      { id: 10, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei10.jpg'},
      { id: 11, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei11.jpg'},
      { id: 12, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei12.jpg'},
      { id: 13, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei13.jpg'},
      { id: 14, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei14.jpg'},
      { id: 15, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei15.jpg'},
      { id: 16, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei16.jpg'},
      { id: 17, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei17.jpg'},
      { id: 18, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei19.jpg'},
      { id: 19, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei20.jpg'},
      { id: 20, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei22.jpg'},
      { id: 21, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei23.jpg'},
      { id: 22, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei24.jpg'},
      { id: 23, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei25.jpg'},
      { id: 24, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei26.jpg'},
      { id: 25, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei27.jpg'},
      { id: 26, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei28.jpg'},
      { id: 27, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei29.jpg'},
      { id: 28, photoSelectCount: 0, status: 1, url:this.data.testDataImgPath +'/mei30.jpg'},],
     iconList:{
       "out": this.data.iconPath+'/out.png',
       'selectadd': this.data.iconPath+'/selectadd.png',
       'selectdelete': this.data.iconPath+'/selectdelete.png',
       "pressDelete": this.data.iconPath+'/pressDelete.png',
       "delete": this.data.iconPath+'/delete.png',
       "pressAdd": this.data.iconPath+'/pressAdd.png',
       "jenter": this.data.iconPath+'/enter.png',
       'jdown': this.data.iconPath+'/down.png',
       "menuadd": this.data.iconPath +"/menuadd.png",
       'smallDelete': this.data.iconPath+'/smallDelete.png',
       'smalladd': this.data.iconPath+'/smalladd.png',
       'smallnotDelete': this.data.iconPath+'/smallnotDelete.png',
       'order': this.data.iconPath+'/order.png',
       'product': this.data.iconPath+'/product.png',
       'menudelete': this.data.iconPath+'/menudelete.png',
      },
     product: { 
       id: 22, name: '实木相框挂墙16寸', 'optionalPhotoNum': 30, 'url': this.data.iconPath +'/sellerportrait.png', 
     'photos': [
       { id: 6, photoSelectCount: 1, productPhoto_id:1},
       { id: 7, photoSelectCount: 2, productPhoto_id:2 },
       { id: 8, photoSelectCount: 3, productPhoto_id:3},
       { id: 9, photoSelectCount: 1, productPhoto_id:4 }               
        ]
      }
    };
    
    this.setData({
      product:data.product,
      photoList: data.photoList,
      iconList: data.iconList
    })

    this.wasSelectCount();
    this.assignmentToPhotoLsit();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})